/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.sys.api;

import com.arisgo.cloud.auth.sys.service.UserService;
import com.arisgo.cloud.auth.sys.vo.UserVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.web.param.Paging;
import com.arisgo.cloud.web.param.Search;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户 Api
 *
 * @author Arisgo
 * @since 2024-08-03
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Tag(name = "用户 API", description = "用户CRUD操作")
public class UserApi {

    private final UserService userService;

    /**
     * 依id查询用户
     */
    @GetMapping(value = "/{id}")
    @Operation(summary = "依id查询")
    public Result<UserVo> findById(@PathVariable(value = "id") Long id) {
        return Result.success(userService.findUserById(id));
    }

    /**
     * 分页查询用户
     */
    @PostMapping(value = "/page")
    @Operation(summary = "分页查询")
    public Result<Pagination<UserVo>> page(@RequestBody Paging params) {
        return Result.success(userService.pageUser(params));
    }

    /**
     * 条件查询用户
     */
    @PostMapping(value = "/find")
    @Operation(summary = "条件查询")
    public Result<List<UserVo>> find(@RequestBody Search param) {
        return Result.success(userService.findUserByParams(param));
    }

    /**
     * 保存用户
     */
    @PostMapping
    @Operation(summary = "新增数据")
    public Result<Boolean> save(@RequestParam(value = "account") String account) {
        return Result.success(userService.insert(account));
    }

    /**
     * 更新用户
     */
    @PutMapping(value = "/{id}")
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@PathVariable(value = "id") Long id, @RequestParam(value = "status") Integer status) {
        return Result.success(userService.modify(id, status));
    }

    /**
     * 逻辑删除用户
     */
    @DeleteMapping(value = "/{id}")
    @Operation(summary = "单个逻辑删除")
    public Result<Boolean> delete(@PathVariable(value = "id") Long id) {
        return Result.success(userService.drop(id));
    }

    /**
     * 逻辑删除用户
     */
    @DeleteMapping
    @Operation(summary = "批量逻辑删除")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return Result.success(userService.drop(ids));
    }

    /**
     * 物理删除用户
     */
    @DeleteMapping(value = "/remove/{id}")
    @Operation(summary = "单个物理删除")
    public Result<Boolean> remove(@PathVariable(value = "id") Long id) {
        return Result.success(userService.del(Lists.newArrayList(id)));
    }

    /**
     * 物理删除用户
     */
    @DeleteMapping(value = "/remove")
    @Operation(summary = "批量物理删除")
    public Result<Boolean> remove(@RequestBody List<Long> ids) {
        return Result.success(userService.del(ids));
    }


    /**
     * 账号锁定/解锁
     */
    @PutMapping(value = "/isLock/{id}")
    @Operation(summary = "账号锁定/解锁")
    public Result<Boolean> isLock(@PathVariable(value = "id") Long id, @RequestParam(value = "status") Integer status) {
        return Result.success(userService.isLock(id, status));
    }

}
